package com.ht.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import com.ht.model.Person;
import com.ht.service.PersonService;
import com.ht.util.Constants;
import com.ht.util.MyPage;

public class PersonAction extends ActionSupport {

	Person person ;
	PersonService personService ;

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public PersonService getPersonService() {
		return personService;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public String doAdd() throws  Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		int flag = personService.addPerson(person);
		PrintWriter out = response.getWriter();
		String target =null; 
		if (flag == 0) {
			request.setAttribute("info", "Failed to add......");
			target = "failure";
		} else if (flag == 1) {
			request.setAttribute("info", "Add items Success......");
			request.setAttribute("path", getBasePath(request) + Constants.PERSON_ACTION);
			target="success";
		} else {
			request.setAttribute("info", "Already exist......");
			target = "failure";
		}
		return target;
	}
	
	public String delete() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		int flag = personService.deletePerson(person);
		PrintWriter out = response.getWriter();
		String target =null; 
		if (flag == 0) {
			request.setAttribute("info", "Failed to delete......");
			target = "failure";
		} else {
			request.setAttribute("info", "Delete items Success......");
			request.setAttribute("path", getBasePath(request) + Constants.PERSON_ACTION);
			target="success";
		}
		return target;
	}

	
	public String toUpdate()throws Exception  {
		Person sa = personService.getPersonById(person);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("person", sa);
		return "edit";
	}
	

	public String doUpdate() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = response.getWriter();
		System.out.println(person.getName());
		int flag = personService.updatePerson(person);
		
		String target =null; 
		if (flag == 0) {
			request.setAttribute("info", "Failed to update......");
			target = "failure";
		} else {
			request.setAttribute("info", "Update items Success......");
			request.setAttribute("path", getBasePath(request) + Constants.PERSON_ACTION);
			target="success";
		}
		return target;
	}

	public String select() throws Exception {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String personId = request.getParameter("id");
		String name = request.getParameter("name");
		Person person = new Person();
		if (personId != null && !"".equals(personId)) {
			person.setId(personId);
		}
		if (name != null && !"".equals(name)) {
			person.setName(name);
		}
		String tempPage = request.getParameter("curPage");
		int curPage = 1;
		if(tempPage != null){
			curPage = new Integer(tempPage);
		}
		MyPage mypage = personService.selectByCon(person, curPage);
		request.setAttribute("mypage", mypage);
		return "index";
		

	}
	
	public String check()throws Exception  {
		Person sa = personService.getPersonById(person);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("person", sa);
		return "check";
	}


	public String getBasePath(HttpServletRequest request) {
		String basePath = request.getScheme() + "://" + request.getServerName()+ ":" + request.getServerPort() + request.getContextPath();
		return basePath;
	}
	
	public String toChose()throws Exception  {

		return "tochose";
	}

}
